/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package srac.controleatividades.cgt;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.List;
import srac.controleatividades.cdp.Aluno;
import srac.controleatividades.cdp.Etapa;
import srac.controleatividades.cdp.StatusSubmissao;

/**
 *
 * @author gmiranda
 */
public class FinalToPdf {
    
    /**
     * Gera o relatório final da etapa com o total de aprovados/reprovados
     * @param alunos
     * @param creditos
     * @param e
     * @return O arquivo PDF em array de bytes
     * @throws BadElementException
     * @throws MalformedURLException
     * @throws IOException
     * @throws DocumentException 
     */

    public static byte[] gerarPdf(List<Aluno> alunos, List<Integer> creditos, Etapa e) throws BadElementException, MalformedURLException, IOException, DocumentException {
        Document document = new Document();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);


        document.open();


        Font f = new Font();
        f.setSize(18);

        Font f2 = new Font();
        f2.setSize(10);

        document.add(new Paragraph("\r\n"));
        Paragraph p;
        p = new Paragraph("Relatório de Final", f);


        p.setAlignment(Element.ALIGN_CENTER);

        document.add(p);
        
        Calendar c = e.getDataInicioSubmissao();
        
        c.set(Calendar.MONTH, c.get(Calendar.MONTH)+1);
        if(c.get(Calendar.MONTH)==12)
        {
            c.set(Calendar.MONTH,1);
        }

        String dataIni = "Incio da Etapa:  " + c.get(Calendar.DATE)+"/"
                +c.get(Calendar.MONTH)+"/"+c.get(Calendar.YEAR);
        document.add(new Paragraph(dataIni, f2));
        
        
        c=e.getDataFimValidacao();
        
        c.set(Calendar.MONTH, c.get(Calendar.MONTH)+1);
        if(c.get(Calendar.MONTH)==12)
        {
            c.set(Calendar.MONTH,1);
        }
        
        
        document.add(new Paragraph("Fim da Etapa:  " + e.getDataFimValidacao().get(Calendar.DATE)+"/"
                +e.getDataFimValidacao().get(Calendar.MONTH)+"/"+e.getDataFimValidacao().get(Calendar.YEAR), f2));
              
           
        document.add(new Paragraph("\r\n"));
        
        p = new Paragraph("Alunos", f);
        p.setAlignment(Element.ALIGN_CENTER);
        document.add(p);
        document.add(new Paragraph("\r\n"));

        PdfPTable table = new PdfPTable(new float[]{0.50f,0.20f, 0.30f});
        table.setHorizontalAlignment(Element.ALIGN_CENTER);


        PdfPCell cell = new PdfPCell(new Paragraph("Nome"));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("Créditos Obtidos"));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("Situação Final"));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        Integer somaA=0;
        Integer somaR=0;
        int index = 0;
        for (Aluno a : alunos) {
            cell = new PdfPCell(new Paragraph(a.getNome(), f2));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);

            table.addCell(cell);

            cell = new PdfPCell(new Paragraph(creditos.get(index).toString(), f2));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            
            if(a.getStatusSubmissao().equals(StatusSubmissao.ATIVIDADES_ACEITAS))
            {
                cell = new PdfPCell(new Paragraph("Atividade Aceitas", f2));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);
                somaA++;
            }
            else
            {
                cell = new PdfPCell(new Paragraph("Atividade Rejeitadas", f2));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);
                somaR++;
            }
            index++; 
        }
        
        document.add(table);
        
        document.add(new Paragraph("\r\n"));
        
        p = new Paragraph("Total de aceitos:  " + somaA.toString() , f2);
        
        p.setAlignment(Element.ALIGN_RIGHT);
        
        document.add(p);    
        
        p = new Paragraph("Total de rejeitados:  " + somaR.toString() , f2);
        
        p.setAlignment(Element.ALIGN_RIGHT);
        
        document.add(p);    
        
        document.close();

        return baos.toByteArray();
    }
}

